package cn.st4rlight.util.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

import cn.st4rlight.util.time.DateTimes;

/**
 * 用于easy-excel中 13位时间戳 --> 日期
 *
 * @author st4rlight <st4rlight@163.com>
 * Created on 2024-07-27
 */
public class DateConverter implements Converter<Long> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return Long.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Long> context) throws Exception {
        Long value = context.getValue();
        String dateString = DateTimes.from(value).formatter(DateTimes.DATE_PATTERN).toString();
        return new WriteCellData<>(CellDataTypeEnum.STRING, dateString);
    }
}
